package com.hopedove.workshop.ui.adapter

import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.Nullable
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.hopedove.workshop.R
import com.hopedove.workshop.data.pojo.ObservableTakeInventoryItem
import com.hopedove.workshop.databinding.WorkshopTakeinventoryListItemBinding

class TakeInventoryDetailAdapter  : RecyclerView.Adapter<TakeInventoryDetailAdapter.VH>() {

    var data: List<ObservableTakeInventoryItem> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val binding: WorkshopTakeinventoryListItemBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.workshop_takeinventory_list_item,
            parent,
            false
        )

        return VH(binding)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        holder.render(data, position)
        //holder.binding.tvOnMachine.tag=1

        holder.binding.tvOnMachine?.addTextChangedListener(MyTextWatcher(1,holder.binding.tvOnMachine))

    }

    override fun getItemCount(): Int = data.size

    fun updateDataSet(list: List<ObservableTakeInventoryItem>) {
        data = list
        notifyDataSetChanged()
    }

    class VH(val binding: WorkshopTakeinventoryListItemBinding) : RecyclerView.ViewHolder(binding.root) {

        fun render(data: List<ObservableTakeInventoryItem>?, position: Int) {
            data?.let {
                val item = it[position]
                binding.item = item
            }
        }

    }


}

class MyTextWatcher(private val decimalDigits:Int, private val editText: EditText) :
    TextWatcher {
    override fun afterTextChanged(s: Editable?) {
//        if(editText.tag==1) {
//            val ObservableTakeInventoryItem=mydata[position]
//            mydata.map{
//                if(it.clothtktNo==ObservableTakeInventoryItem.clothtktNo){
//                    it.onMachine.set(s.toString())
//                }
//
//            }
//        }

    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        val text = s?.toString()
        if (text == "."){
            //第一位不能位 .
            editText.setText("")
        }else if (text == "00"){
            //不能输入00
            editText.setText("0")
            editText.setSelection(1)
        }else if (!text.isNullOrEmpty()){
            val decimalArray = text.split(".")
            if (decimalArray.size == 2){
                //存在小数点后 限制最大输入长度
                val decimal = decimalArray[1]
                //限制最大长度为当前文本长度+(限制小数位数-已输入的小数位数)
                editText.setMaxLength(text.length+(decimalDigits-decimal.length))
            }else{
                //没有小数点设置最大位数为100
                editText.setMaxLength(100)
            }
        }
    }
}
//自定义TextView扩展函数

fun TextView.setMaxLength(length:Int){
    this.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(length))
}
